package com.cy.pj.activity.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cy.pj.activity.pojo.Activity;
import com.cy.pj.activity.service.ActivityService;

@Controller
public class ActivityController {
	@Autowired
	private ActivityService activityService;

	@RequestMapping("/activity/doFindById/{id}")
	public String doFindById(@PathVariable("id") long id, Model model) {
		
		Activity activity = activityService.findById(id);
		System.out.println("act="+activity);
		model.addAttribute("activity", activity);
		return "activity_update";
	}

	@RequestMapping("/activity/doUpdateObject")
	public String doUpdateObject(Activity activity) {
		activityService.updateObject(activity);
		return "redirect:/activity/doFindActivitys";
	}

	@RequestMapping("/activity/doDeleteObject/{id}")
	public String doDeleteById(@PathVariable long id) {
		activityService.deleteById(id);
		// System.out.println("delete.id"+id);
		return "redirect:/activity/doFindActivitys";
	}

	@RequestMapping("/activity/doSaveObject")
	public String doSaveObject(Activity activity) {
		// System.out.println("activity"+activity);//检查客户端提交数据
		activityService.saveActivity(activity);
		return "redirect:/activity/doFindActivitys";
	}

	@RequestMapping("/activity/doFindActivitys")
	public String doFindActivitys(Model model) {
		List<Activity> list = activityService.findActivities();
		model.addAttribute("list", list);
		// list.forEach((item)->System.out.println(item));//JDK8 lambda 输出list
		// list.forEach(System.out::println);//JDK8方法引用
		return "activity";
	}
}
